package com.kolon.business.mServiceDesk.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jdom.Document;
import org.jdom.Element;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.kolon.framework.constant.Constant;
import com.kolon.framework.interfaces.SimpleController;
import com.kolon.framework.util.ComUtil;
import com.kolon.framework.util.MsgUtil;

@Controller("com.kolon.business.mServiceDesk.controller.MServiceDeskProcessCat2Controller")
public class MServiceDeskProcessCat2Controller implements SimpleController {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value="/business/mServiceDesk/mServiceDeskProcessCat2.do")
	public ModelAndView controller(HttpServletRequest request,
		HttpServletResponse response) throws Exception {
		// 도메인
		String url = MsgUtil.getMessage("xml.url");
		// 세션 아이디 및 암호 값을 불러옴
		HttpSession httpSession = request.getSession();
		Map userMap = (Map)httpSession.getAttribute(Constant.SESSION_USER_INFO);
		String id = (String)userMap.get("userId");
		String pw = (String)userMap.get("userPw");
		String crType = ComUtil.nullChk(request.getParameter("cr_type"), "I");
		String tid = id;
		//#######################################################################################
		// 상세분류
		//#######################################################################################
		StringBuffer buffer = new StringBuffer();
		buffer.append(url);
		buffer.append("/servicedesk.aspx?mode=").append("5-7");
		buffer.append("&userid=").append(tid);		
		buffer.append("&cr_type=").append(crType);
		buffer.append("&symt1_desc=").append(ComUtil.getEncode(request.getParameter("Cate1"),"EUC-KR"));
		// XML DOCUMENT 및 인증 처리
		Document doc = ComUtil.getDocument(buffer.toString(), id, pw);
		Element element = doc.getRootElement();
		List xmlList = element.getChildren("Table");
		Iterator itXml = xmlList.iterator();
		List mapList = new ArrayList();
		Map map = new HashMap();
		// XML 데이터 
		while (itXml.hasNext()) {
			Element el = (Element)itXml.next();
			map = new HashMap();
			map.put("SYMT_CODE", el.getChild("SYMT_CODE").getText());
			map.put("SYMT_DESC", el.getChild("SYMT_DESC").getText());
			mapList.add(map);
		}
		//#######################################################################################
		Map model = new HashMap();
		model.put("list", mapList);
		return new ModelAndView("jsonView", model);
	}

}